package com.example.demo8.dao.Impl;

import com.example.demo8.dao.ITaskDao;
import com.example.demo8.entity.Task;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ITaskDaoImpl implements ITaskDao {
    @Override
    public List<Task> getAll() throws IOException {
        SqlSession sqlSession = getSqlSession();
        List<Task> taskList = sqlSession.selectList("com.example.demo8.dao.ITaskDao.getAll");
        sqlSession.close();
        return taskList;
    }

    @Override
    public List<Task> getAllByName(String taskName) throws IOException {
        SqlSession sqlSession = getSqlSession();
        List<Task> taskList = sqlSession.selectList("com.example.demo8.dao.ITaskDao.getAllByName",taskName);
        sqlSession.close();
        return taskList;
    }

    @Override
    public void update(Task task) throws IOException {
        SqlSession sqlSession = getSqlSession();
        sqlSession.update("com.example.demo8.dao.ITaskDao.update",task);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public Task getById(Integer taskId) throws IOException {
        SqlSession sqlSession = getSqlSession();
        Task task = sqlSession.selectOne("com.example.demo8.dao.ITaskDao.getById",taskId);
        sqlSession.close();
        return task;
    }

    @Override
    public void delete(Integer taskId) throws IOException {
        SqlSession sqlSession = getSqlSession();
        sqlSession.delete("com.example.demo8.dao.ITaskDao.delete",taskId);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void add(Task task) throws IOException {
        SqlSession sqlSession = getSqlSession();
        sqlSession.insert("com.example.demo8.dao.ITaskDao.add",task);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void deleteAll() throws IOException {
        SqlSession  sqlSession = getSqlSession();
        sqlSession.delete("com.example.demo8.dao.ITaskDao.deleteAll");
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public int getCount() throws IOException {
        SqlSession sqlSession = getSqlSession();
        int count = sqlSession.selectOne("com.example.demo8.dao.ITaskDao.getCount");
        sqlSession.close();
        return count;
    }

    @Override
        public List<Task> getAllByPage(int start, int end) throws IOException {
            SqlSession sqlSession = getSqlSession();
            Map<String, Integer> params = new HashMap<>();
            params.put("start", start);
            params.put("end", end);
            List<Task> taskList = sqlSession.selectList("com.example.demo8.dao.ITaskDao.getAllByPage", params);
            sqlSession.close();
            return taskList;
        }



    private static SqlSession getSqlSession() throws IOException {
        String paeh = "mybatis-config.xml";
        InputStream resourcesAsStream = Resources.getResourceAsStream(paeh);
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(resourcesAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }
}
